package cn.yuanqiao.archive.liyong.service.impl;

import cn.yuanqiao.archive.liyong.domain.YqMyCollect;
import cn.yuanqiao.archive.liyong.mapper.YqMyCollectMapper;
import cn.yuanqiao.archive.liyong.service.IYqMyCollectService;
import cn.yuanqiao.common.constant.Constants;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 我的收藏夹Service业务层处理
 *
 * @ProjectName: JZSDAS
 * @Package: cn.yuanqiao.archive.liyong.service.impl
 * @ClassName: YqMyCollectServiceImpl
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/8/9 15:29
 * @Version: 1.0
 */
@Service
public class YqMyCollectServiceImpl implements IYqMyCollectService {
    @Autowired
    private YqMyCollectMapper yqMyCollectMapper;

    /**
     * 查询我的收藏夹
     *
     * @param id 我的收藏夹主键
     * @return 我的收藏夹
     */
    @Override
    public YqMyCollect selectYqMyCollectByDaId(String id) {
        return yqMyCollectMapper.selectYqMyCollectById(id, new YqMyCollect().getTableName());
    }

    /**
     * 查询我的收藏夹列表
     *
     * @param yqMyCollect 我的收藏夹
     * @return 我的收藏夹
     */
    @Override
    public List<YqMyCollect> selectYqMyCollectList(YqMyCollect yqMyCollect) {
        yqMyCollect.setUserId(String.valueOf(SecurityUtils.getUserId()));
        return yqMyCollectMapper.selectYqMyCollectList(yqMyCollect, new YqMyCollect().getTableName());
    }

    /**
     * 新增我的收藏夹
     *
     * @param yqMyCollect 我的收藏夹
     * @return 结果
     */
    @Override
    public int insertYqMyCollect(YqMyCollect yqMyCollect) {
        yqMyCollect.setUserId(String.valueOf(SecurityUtils.getUserId()));
        List<YqMyCollect> list = yqMyCollectMapper.selectYqMyCollectByDaId(yqMyCollect.getDaId(),
                yqMyCollect.getUserId()
                , new YqMyCollect().getTableName());
        if (list != null && list.size() > 0) {
            throw new ServiceException("已经收藏过了,请勿重复收藏!");
        }
        String tenantCode = SecurityUtils.getTenantCode();

        StringBuilder sql = new StringBuilder();
        sql.append(" select ");
        String tablenametype = yqMyCollect.getSubTableName().substring(yqMyCollect.getSubTableName().length() - 3, yqMyCollect.getSubTableName().length() - 2);
        if ("F".equalsIgnoreCase(tablenametype)) {//档案层
            sql.append(" TITLE from " );
            sql.append(tenantCode + Constants.BIG_TABLE_YQDA+" where DA_ID = '");
            sql.append(yqMyCollect.getDaId()+"' ");
        } else if ("V".equalsIgnoreCase(tablenametype)) {//案卷层
            sql.append(" TITLE from " );
            sql.append(tenantCode + Constants.BIG_TABLE_YQFOLDER+" where FOLDER_ID = '");
            sql.append(yqMyCollect.getDaId()+"' ");
        } else if ("X".equalsIgnoreCase(tablenametype)) {//项目层
            sql.append(" XMMC from " );
            sql.append(tenantCode + Constants.BIG_TABLE_YQITEM+" where ITEM_ID = '");
            sql.append(yqMyCollect.getDaId()+"' ");
        }
        String daContent = yqMyCollectMapper.queryData(sql.toString());
        yqMyCollect.setDaContent(daContent);
        yqMyCollect.setCreateTime(DateUtils.getNowDate());
        yqMyCollect.setUserId(String.valueOf(SecurityUtils.getUserId()));
        yqMyCollect.setUserName(SecurityUtils.getUsername());
        yqMyCollect.setId(String.valueOf(SnowflakeIdGenerator.generateId()));
        yqMyCollect.setType("1");
        yqMyCollect.setCreateTime(new Date());
        return yqMyCollectMapper.insertYqMyCollect(yqMyCollect, new YqMyCollect().getTableName());
    }

    /**
     * 修改我的收藏夹
     *
     * @param yqMyCollect 我的收藏夹
     * @return 结果
     */
    @Override
    public int updateYqMyCollect(YqMyCollect yqMyCollect) {
        yqMyCollectMapper.updateYqMyCollect(yqMyCollect, new YqMyCollect().getTableName());
        return 1;
    }

    /**
     * 批量删除我的收藏夹
     *
     * @param ids 需要删除的我的收藏夹主键
     * @return 结果
     */
    @Override
    public int deleteYqMyCollectByDaIds(String[] ids) {
        return yqMyCollectMapper.deleteYqMyCollectByDaIds(ids, new YqMyCollect().getTableName());
    }

    /**
     * 删除我的收藏夹信息
     *
     * @param id 我的收藏夹主键
     * @return 结果
     */
    @Override
    public int deleteYqMyCollectByDaId(String id) {
        return yqMyCollectMapper.deleteYqMyCollectByDaId(id, new YqMyCollect().getTableName());
    }

    /**
     * 判断收藏权限
     *
     * @return
     */
    @Override
    public boolean queryAuthority() {
        Set<String> permissions = SecurityUtils.getLoginUser().getPermissions();
        boolean contains = permissions.contains("archive:myCollect:add");
        return contains;
    }
}
